$(function () {
  $(".ambitusInfo_scroll").yi_scroll();
  var ydhData = [];
  // 协同框弹出
  $('#alarm').on('click', '.coordinationBtn', function () {
    $("#teamworkModalBox").addClass("is-visible3");
    alarmIndex = $(this).attr('index');
    var arr = newAlarmArr[alarmIndex];
    $('#teamworkModal .address').text(arr.address);
    $('#teamworkModal .alarmType').text(arr.alarmName);
    $('#teamworkModal .alarmTime').text(moment(arr.beginTime).format('YYYY-MM-DD HH:mm:ss'));

    var linkMan1 = arr.linkMain1 ? arr.linkMain1 + '/' + arr.linkPhone1 : '';
    $('#teamworkModal .linkMan1').text(linkMan1);
    console.log(arr);

    // 获取重点人员数据
    $.ajax({
      url: '/answering/getKeyPersonnel?devCode=' + arr.devCode,
      success: function (res) {
        console.log(res);
        if (res.status == 200) {
          var data = res.data;
          // var zdryData = 
          $('#teamworkModal .zdryData').html('');
          for (var i = 0; i < data.length; i++) {
            $('#teamworkModal .zdryData').append('<li>' + data[i].name + '，' + data[i].age + '，' + data[i].gender + '，' + data[i].personnelType + '，' + data[i].telephone + '，' + data[i].address + '</li>');
          }
        }
      }
    });

    $.ajax({
      url: '/answering/getPeripheralEquipment?devCode=' + arr.devCode,
      success: function (res) {
        console.log(res);
        if (res.status == 200) {
          // 周边消防栓
          var xfsData = res.data.消防栓;
          $('#teamworkModal .xfsData').html('');
          $('#teamworkModal .ydhData').html('');
          for (var i = 0; i < xfsData.length; i++) {
            $('#teamworkModal .xfsData').append('<li><span>' + xfsData[i].device + '</span><span class="fr">' + xfsData[i].distance + 'm</span></li>');
          }
          // 周边协同点
          ydhData = res.data.应答盒;
          for (var i = 0; i < ydhData.length; i++) {
            $('#teamworkModal .ydhData').append('<li><label><i></i><input index="' + i + '" type="checkbox" style="display: none;"></label><span class="">' + ydhData[i].device + '</span><span class="fr">' + ydhData[i].distance + 'm</span></li>');

          }
        }
      }
    });
  });

  // 协同内容 点击打勾
  $("#teamworkModal .cooContentTabBox input[type='checkbox']").change(function () {
    $('#teamworkModal .cooContentTabBox input[type=checkbox]:checked').each(function (i, item) {
      console.log(this);
      $(this).siblings('i').addClass('active');

    });
    $('#teamworkModal .cooContentTabBox input[type=checkbox]').not("input:checked").each(function (i, item) {
      console.log(this);
      $(this).siblings('i').removeClass('active');
    });
  });
  // 周边协同点 点击打勾
  $("#teamworkModal .ydhData").on('change', "input[type='checkbox']", function () {
    $('#teamworkModal .ydhData input[type=checkbox]:checked').each(function (i, item) {
      console.log(this);
      $(this).siblings('i').addClass('active');

    });
    $('#teamworkModal .ydhData input[type=checkbox]').not("input:checked").each(function (i, item) {
      console.log(this);
      $(this).siblings('i').removeClass('active');
    });
  });

  //  关闭窗口
  $("#teamworkModalBox").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
      teamworkModalClearFn();
    }
  });

  // 发送协同信息
  $('#teamworkModal .sendBtn').on('click', function () {
    var orgId = localStorage.getItem('userOrgId');
    var sendArr = [];
    var content = ''
    $("#teamworkModal .ydhData input[type=checkbox]:checked").each(function () {
      var index = $(this).attr('index');
      sendArr.push(ydhData[index]);
    });
    $("#teamworkModal .cooContentTabBox input[type=checkbox]:checked").each(function () {
      content += $(this).parent().siblings('span').text() + ' ';
    });
    var obj = {};
    var arr = newAlarmArr[alarmIndex];
    obj.manualHistory = sendArr;
    obj.synergeticContent = content;
    obj.initiator = localStorage.getItem('userName');
    obj.id = arr.alarmId;
    console.log($('#teamworkModal .remark textarea').val())
    obj.remarks = $('#teamworkModal .remark textarea').val();
    jsonData = JSON.stringify(obj);
    console.log(jsonData);
    $.ajax({
      url: '/workOrder/apkSave?orgid=' + orgId,
      type: 'POST',
      async: false,
      data: jsonData,
      dataType: 'json',
      contentType: 'application/json',
      success: function (res) {
        console.log(res);
        if (res.status == 200) {
          $("#teamworkModalBox").removeClass("is-visible3");
          teamworkModalClearFn();
        }
      }
    })
  });
  // 协同信息取消按钮
  $('#teamworkModal .cancelBtn').on('click', function () {
    $("#teamworkModalBox").removeClass("is-visible3");
    teamworkModalClearFn();
  });
  function teamworkModalClearFn(){
    // 清除数据
    $("#teamworkModalBox .ydhData input[type=checkbox]").prop("checked", false);
    $("#teamworkModal .cooContentTabBox input[type=checkbox]").prop("checked", false);
    $("#teamworkModalBox .ambitusInfoItem label").find('i').removeClass('active');
    $("#teamworkModalBox .remark").find("textarea").val('');
  }
});